package com.killLog.service.impl;

import com.killLog.common.DataStatusEnum;
import com.killLog.common.PsyConstants;
import com.killLog.common.page.Page;
import com.killLog.dao.UserDao;
import com.killLog.dto.UserDto;
import com.killLog.exception.BusinessException;
import com.killLog.model.Role;
import com.killLog.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
/**
用户业务层实现类
*/
@Service
public class UserServiceImpl implements UserService {
    /**
    依赖注入
    */
    @Autowired
    private UserDao userDao;
    /**
     * @Description: 抽取出来的删除单个用户的方法，提高复用性
     * @Param: uId 
     * @return void
    */
    private void deleteMethod(String uId) {
        //1.根据传入的id，判断用户是否存在且用户状态是否满足删除的条件
        boolean ok = userDao.existsValidStatus(uId,DataStatusEnum.UNDETERMINED.getCode());
        if (!ok) {
            throw new BusinessException("账号不存在或用户状态不允许删除！删除失败！");
        }
        //2.满足条件，进行删除
        userDao.delete(uId);
    }
    
    
    /**
    查询用户列表
    */
    @Override
    public Page getPage(UserDto userDto) {
        //调用方法完成分页查询
        return Page.getPage(userDto, () -> userDao.findList(userDto));
    }
    /**
    更新用户状态信息
    */
    @Override
    public String executeChangeStatus(String uId, String currentStatus) {
        String message = null;//操作提示信息
        String targetStatus = null;//要更新的目标状态
        if (DataStatusEnum.UNDETERMINED.getCode().equals(currentStatus)) {
            targetStatus = DataStatusEnum.DETERMINED.getCode();
            message = "确定";
        } else if (DataStatusEnum.DETERMINED.getCode().equals(currentStatus)) {
            targetStatus = DataStatusEnum.ACTIVE.getCode();
            message = "启用成功";
        } else if (DataStatusEnum.ACTIVE.getCode().equals(currentStatus)) {
            targetStatus = DataStatusEnum.DISABLED.getCode();
            message = "禁用成功";
        } else if (DataStatusEnum.DISABLED.getCode().equals(currentStatus)) {
            targetStatus = DataStatusEnum.ACTIVE.getCode();
            message = "启用成功";
        }
        if (targetStatus==null) {
            throw new BusinessException("用户当前状态未知，不能做修改");
        }
        //调用dao实现更改：需要三个参数
        int rows = userDao.updateStatus(uId,currentStatus,targetStatus);
        if (rows > 0) {
            return message;
        }
        throw new BusinessException("更新用户状态失败！");
    }
    /**
    新增用户
    */
    @Override
    public void executeAdd(UserDto userDto) {
        //1.根据传入的账号信息中的id，判断该账号是否在数据库表中已经存在
        boolean exists = userDao.existsOne(userDto.getU_id());
        if (exists) {
            //已经存在
            throw new BusinessException("账号已经存在！");
        }
        //2.不存在，正常添加
        userDto.setU_status(DataStatusEnum.UNDETERMINED.getCode());//手动补全缺失的用户状态：默认00未确定
        userDto.setU_pwd(PsyConstants.USER_DEFAULT_PASSWORD);
        int rows = userDao.insertOne(userDto);
        if (rows != 1) {//说明新增用户失败了
            throw new BusinessException("添加账号出现未知错误！");
        }
    }
    /**
    删除用户
    */
    @Override
    public void executeDelete(String uId) {
        //调用方法实现删除单个用户
        deleteMethod(uId);
    }
    /**
     * @Description: 批量删除用户
     * @Param: uIds 
     * @return void
    */
    @Override
    public void executeDelete(String[] uIds) {
        for (String uId : uIds) {
            //循环执行删除单个用户的方法即可实现批量删除用户
            deleteMethod(uId);
        }
    }
    /**
     * @Description: 获取用户角色列表
     * @Param: userDto 
     * @return com.killLog.common.page.Page<com.killLog.model.Role>
    */
    @Override
    public Page<Role> getUserRolePage(UserDto userDto) {
        return Page.getPage(userDto,()->userDao.findRoleList(userDto.getU_id(),DataStatusEnum.ACTIVE.getCode()));
    }
    /**
     * @Description: 
     * @Param dto 编辑用户
     * @return void
    */
    @Override
    public void execEdit(UserDto dto) {
        boolean ok = userDao.existsValidStatus(dto.getU_id(),DataStatusEnum.UNDETERMINED.getCode());
        if(!ok){
            throw new BusinessException("账号不存在或数据不允许更新！");
        }
        userDao.updateOne(dto);
    }
    /**
     * @Description: 变更用户角色
     * @Param u_id 
     * @Param ro_id 
     * @Param has 
     * @return void
    */
    @Override
    public void switchUserRole(String u_id, Integer ro_id, boolean has) {
        if(has){
            userDao.insertUserRoles(u_id, new Integer[]{ro_id});
        }else{
            userDao.deleteUserRole(u_id,ro_id);
        }
    }
}
